package com.threeti.jiayusms.domain.service.defaulttemplate;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.threeti.jiayusms.domain.model.defaulttemplate.DefaultCommunicationTemplate;
import com.threeti.jiayusms.domain.model.defaulttemplate.SMSTemplateMapping;
import com.threeti.jiayusms.domain.model.defaulttemplate.VoiceTemplateMapping;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.infrastructure.defaulttemplate.DefaultCommunicationTemplateDao;
import com.threeti.jiayusms.infrastructure.defaulttemplate.SMSTemplateMappingDao;
import com.threeti.jiayusms.infrastructure.defaulttemplate.VoiceTemplateMappingDao;

@Service
@Transactional(readOnly=true)
public class DefaultCommunicationTemplateQueryServiceImpl implements DefaultCommunicationTemplateQueryService {

  @Autowired
  private DefaultCommunicationTemplateDao dao;
  
  @Autowired  
  private SMSTemplateMappingDao smsMappingDao;
  
  @Autowired  
  private VoiceTemplateMappingDao voiceMappingDao;

  @Override
  public PagedListHandler<DefaultCommunicationTemplate> findAllDefaultTemplatesByPage(int currentPageIndex,
                                                                                      int startingIndex) {
    return dao.findAllDefaultTemplatesByPage(currentPageIndex, startingIndex);
  }

  @Override
  public DefaultCommunicationTemplate findDefaultTemplateBy(Long templateId) {
    DefaultCommunicationTemplate template = dao.loadBy(templateId);

    dao.forceInitialize(template.getSmsTemplateMappings());
    List<SMSTemplateMapping> smsMappings = template.getSmsTemplateMappings();
    for (SMSTemplateMapping smsMapping : smsMappings) {
      dao.forceInitialize(smsMapping.getMsgTemplate());
      dao.forceInitialize(smsMapping.getMsgRuleTemplate());
      dao.forceInitialize(smsMapping.getMsgTimeTemplate());
    }
    
    dao.forceInitialize(template.getVoiceTemplateMappings());
    List<VoiceTemplateMapping> voiceMappings = template.getVoiceTemplateMappings();
    for (VoiceTemplateMapping voiceMapping : voiceMappings) {
      
      dao.forceInitialize(voiceMapping.getVoiceTemplate());
      dao.forceInitialize(voiceMapping.getVoiceRuleTemplate());
      dao.forceInitialize(voiceMapping.getVoiceTimeTemplate());
    }
    
    return template;
  }

  @Override
  public DefaultCommunicationTemplate findDefaultTemplateBy(String templateId) {
    return dao.loadBy(templateId);
  }

  @Override
  public PagedListHandler<DefaultCommunicationTemplate> findDefaultTemplatesByPage(DynaSearchCriteria dynaSearchCriteria,
                                                                                   int currentPageIndex,
                                                                                   int startingIndex) {
    if (!dynaSearchCriteria.isAnyConditionInput()) {//无条件查询
      return dao.findAllDefaultTemplatesByPage(currentPageIndex, startingIndex);
    }    
    return dao.findDefaultTemplatesByPage(dynaSearchCriteria, currentPageIndex, startingIndex);
  }

  @Override
  public SMSTemplateMapping findSMSTemplateMappingBy(Long smsMappingId) {
    SMSTemplateMapping smsMapping = smsMappingDao.loadBy(smsMappingId);
    
    dao.forceInitialize(smsMapping.getMsgTemplate());
    dao.forceInitialize(smsMapping.getMsgRuleTemplate());
    dao.forceInitialize(smsMapping.getMsgTimeTemplate());
    
    return smsMapping;
  }

  @Override
  public VoiceTemplateMapping findVoiceTemplateMappingBy(Long voiceMappingId) {
    VoiceTemplateMapping voiceMapping = voiceMappingDao.loadBy(voiceMappingId);
    
    dao.forceInitialize(voiceMapping.getVoiceTemplate());
    dao.forceInitialize(voiceMapping.getVoiceRuleTemplate());
    dao.forceInitialize(voiceMapping.getVoiceTimeTemplate());
    
    return voiceMapping;
  }
  
}
